//
//  nearCollectionViewCell.m
//  INAlienLand
//
//  Created by dllo on 15/7/6.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "nearCollectionViewCell.h"
#import "HTTPTool.h"
#import "nearmodel.h"
#import "downTableViewCell.h"
#import "UIImageView+WebCache.h"





@implementation nearCollectionViewCell


-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.shishi  =[[UIView alloc]init];
        self.shishi.tintColor = [UIColor blackColor];
        [self.contentView addSubview:self.shishi];
        
        
        
        
    }
    return  self;
}
-(void)layoutSubviews
{
    self.shishi.frame = CGRectMake(0, 0, self.contentView.frame.size.width, self.contentView.frame.size.height);
    
}
-(void)setStr:(NSString *)str
{
    _str = str;
    self.nearArr =[NSMutableArray array];
    self.strArr = [NSMutableArray array];
    [self createData];
    [self createTable];
    
   
    
}
-(void)createData
{
    [HTTPTool get:[NSString stringWithFormat:@"http://api.breadtrip.com/place/pois/nearby/?keyword=&category=%@&start=0&count=20&latitude=38.883823&longitude=121.545036&",self.str] body:nil HTTPResult:JSON success:^(id result) {
        NSDictionary *Dic =result;
        NSArray *itemsArr = Dic[@"items"];
               for (NSDictionary *tempDic in itemsArr) {
            nearmodel *nearPlay = [[nearmodel    alloc]init];
            [nearPlay setValuesForKeysWithDictionary:tempDic];
            [self.nearArr addObject:nearPlay];
            NSArray *tipArr = tempDic[@"tips"];
            for (NSDictionary *tipDic in tipArr) {
                NSString *str = tipDic[@"content"];
                if (str) {
                    [self.strArr addObject:@"暂无点评"];
                }else
                {
                [self.strArr addObject:str];
                }
            }
            
            
            
        }
        [self.table reloadData];
    } failure:^(NSError *error) {
        
    }];
}
-(void)createTable
{
    self.table = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.contentView.frame.size.width, self.contentView.frame.size.height) style:UITableViewStylePlain];
    [self.contentView addSubview:self.table];
    self.table.delegate = self;
    self.table.dataSource = self;
    
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return self.nearArr.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 150;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reuse = @"reuse";
    downTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        cell= [[downTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse];
    }
    nearmodel *near = self.nearArr[indexPath.row];
    [cell.leftimageView sd_setImageWithURL:[NSURL URLWithString:near.cover_s]];
    cell.titleLabel.text=near.name;
    float floatString = [near.distance floatValue];
    
    cell.min_price.text = [NSString stringWithFormat:@"距我%.0fm",floatString*1000];
    cell.market_price.text = [NSString stringWithFormat:@"%@去过",near.visited_count];
    int floatString1 = [near.tips_count intValue];
    if (floatString1==0) {
        cell.depart_dates.text = [NSString stringWithFormat:@"暂无点评"];
    }else
    {
    cell.depart_dates.text = [NSString stringWithFormat:@"%@点评",near.tips_count];
    }
   
    return cell;
}
@end
